

layui.use(['table', 'HttpRequest', 'func', 'form','util'], function () {



    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    var form = layui.form;
    var util = layui.util;

    /**
     * 初始化参数
     */
    var Device = {
        tableId: "openDeviceTable"
    };

    /**
     * 初始化表格的列
     */
    Device.initColumn = function () {
        return [[
            {type: 'radio'},
            {field: 'deviceSn', sort: true, align: "center", title: '设备编号'},
            {field: 'deviceBrand', sort: true, align: "center", title: '设备厂家'},
            {
                field: 'deviceType', sort: true, align: "center", title: '设备类型', templet: function (data) {
                    if (data.deviceType === 1) {
                        return '塔吊';
                    } else if(data.deviceType === 2){
                        return '升降机';
                    }else if(data.deviceType === 3){
                        return '吊钩可视化';
                    }else if(data.deviceType === 4){
                        return 'Al服务器';
                    }else if(data.deviceType === 5){
                        return '监控摄像头';
                    }else if(data.deviceType === 6){
                        return '电检测设备';
                    }
                }
            }
        ]];
    };





    /**
     * 点击查询按钮
     */
    Device.search = function () {
        var queryData = {};
        queryData['deviceSn'] = $("#deviceSnInput").val();
        console.log("搜索" + JSON.stringify(queryData))

        table.reload(Device.tableId, {
            where: queryData, page: {curr: 1}
        });
    };




    layui.use('table' ,function () {
        var table = layui.table
        // 监听选中行
        table.on('row(openDeviceTable)',function (obj) {
            var deviceBrandId = obj.data.deviceBrandId
            var deviceSn = obj.data.deviceSn
            var deviceType = obj.data.deviceType
            // 如果是iframe层
            parent.layui.$("#deviceSn").val(deviceSn)
            parent.layui.$("#deviceType").val(deviceType)
            window.parent.layui.$("#deviceBrand").val(deviceBrandId)
            parent.layui.form.render("select")
            //当在iframe页面关闭自身时，在iframe页执行以下js脚本
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        })

        // 监听选中按钮
        table.on('radio(openDeviceTable)', function (obj) { //监听复选框,获取选择到的值
            var deviceBrandId = obj.data.deviceBrandId
            var deviceSn = obj.data.deviceSn
            var deviceType = obj.data.deviceType
            // 如果是iframe层
            parent.layui.$("#deviceSn").val(deviceSn)
            parent.layui.$("#deviceType").val(deviceType)
            window.parent.layui.$("#deviceBrand").val(deviceBrandId)
            parent.layui.form.render("select")
            // 如果是页面层
            // $('#projectName').val(projectName)
            // $('#projectId').val(projectId)

            //当在iframe页面关闭自身时，在iframe页执行以下js脚本
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            // var  data1 = parent.layui.$('#projectName').val()
            // var  data2 =parent.layui.$('#projectId').val()
            // console.log(data1 +"==" +data2)
            // form.val("project",projectName);   //这样写才能渲染
            // location.reload();
            // 赋值之后要刷新表单
            // form.render(null,'project')
            // layer.close(layer.index);
        })
    })









    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Device.tableId,
        url: Feng.ctxPath + '/device/findPage',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Device.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData
    });

    // 搜索按钮点击事件
    $('#btnDevSearch').click(function () {
        Device.search();
    });


    // 工具条点击事件
    table.on('tool(' + Device.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Device.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Device.onDeleteItem(data);
        }
    });

});
